<?php
function checkDb($x, $y, $dbXY)
{
$poly_points = $dbXY;
//echo $poly_points[0].", ".$poly_points[1]." ".$poly_points[2];
$polySides = count($poly_points);
//echo "Number of sides: ".$polySides;
$empty_count = 0;
 // Split all poly_points in arrays
  for ($k=0; $k<$polySides; $k++) {
    // discart empty
    if (trim($poly_points[$k])=="") {
      $empty_count++;
      continue;
    }
    $tmp_pp = explode(',', $poly_points[$k]);
	//error throwing due to value being less than zero???
	//echo "points: ".$tmp_pp[0].",".$tmp_pp[1]."\n";
    $polyX[] = trim($tmp_pp[0]); 
    $polyY[] = trim($tmp_pp[1]);
  }
  //echo "Length of x points: ".count($polyX);  
  $polySides -= $empty_count;
 $result=false;
  $intercept=0;
  //echo $polySides."\n";

  // Way 2
  for ($i=0,  $j=$polySides-1; $i<$polySides; $j=$i++){
    if (
	((($polyY[$i] <= $y) && ($y < $polyY[$j])) || (($polyY[$j] <= $y) && ($y < $polyY[$i]))) &&
	($x < ($polyX[$j] - $polyX[$i]) * ($y - $polyY[$i]) / ($polyY[$j] - $polyY[$i]) + $polyX[$i])
	)
	  $result = !$result;
	  //echo $result;
  }
  return $result;
}

?>
